using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


using System.Data.Linq;
using System.Linq;

using DataLayer;

namespace BusinessLayer.Activities
{
   public partial class Delete_Widget : System.Workflow.ComponentModel.Activity
   {
      public Delete_Widget()
      {
         //InitializeComponent();
      }

      private static DependencyProperty WidgetIdProperty = DependencyProperty.Register("WidgetId", typeof(int), typeof(Delete_Widget));
      public int WidgetId
      {
         get { return (int)base.GetValue(WidgetIdProperty); }
         set { base.SetValue(WidgetIdProperty, value); }
      }

      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
         using (var db = DatabaseHelper.GetDashboardData())
         {
            var w = db.TabWidgets.Single(p => p.Id == WidgetId);
            db.TabWidgets.DeleteOnSubmit(w);

            //get all widgets in this tab and this column and reorder them if their order no > current widget order no
            var widgets = from p in db.TabWidgets
                          where p.Id != WidgetId && p.ColumnNo == w.ColumnNo && p.OrderNo > w.ColumnNo && p.TabId == w.TabId
                          select p;

            foreach (var obj in widgets)
            {
               obj.OrderNo -= 1;
            }

            db.SubmitChanges();
         }
         return ActivityExecutionStatus.Closed;
      }
   }
}